---
title: "Schäfer_BJPS_Data_preparation_ESS 9"
author: "Armin Schäfer, WWU Münster"
date: |
  `r lubridate::today()`
output: html_document
---

This R Markdown (Rmd) file contains material to reproduce the results reported in Schäfer "Cultural Backlash? How not to explain the rise of authoritarian populism." In their book, Norris and Inglehart use ESS data from the waves 1 to 7. I complement their analyses with the most recent **European Social Survey wave 9 from 2018**:

*ESS Round 9: European Social Survey Round 9 Data (2018). Data file edition 3.1. NSD---Norwegian Centre for Research Data, Norway---Data Archive and distributor of ESS data for ESS ERIC. [doi:10.21338/NSD-ESS9-2018](http://dx.doi.org/10.21338/NSD-ESS9-2018)*. 

This script prepares the data for further analyses. The analyses reported in the paper can be found in the file "replication_figures_tables.Rmd". Note, however, as this script always loads the newest version of the ESS data, [updates](https://www.europeansocialsurvey.org/data/alerts.html) might lead to slight changes in the results and therefore differ slightly from the published version.

*Note*: I rely on the [R package management tool `pacman`](). Please run `install.packages("pacman")` to install the package from CRAN. The command `pacman::p_load()` will then import all specified packages and install them first in case they are currently not installed on your machine.

Packages and functions
```{r load packages, message = FALSE}

if (!require("pacman")) install.packages("pacman")
pacman::p_load(tidyverse, essurvey, sjlabelled, data.table, psych,
               openxlsx, here, patchwork, modelsummary)

tabl <- function(...) table(..., useNA='ifany')

```

```{r setup, include=FALSE}

knitr::opts_chunk$set(echo = TRUE, warning = FALSE, message = FALSE)

```

Get the data. Load the dataset and prepare for analyses. To access ESS data, you have to register first. Go to the [ESS website](http://www.europeansocialsurvey.org/user/new) and provide your details.

```{r load data, results="hide"}

# This script only works if you have registered to access European Social Survey data.

essurvey::set_email("your email")

ess_raw <- import_rounds(9, format="spss")

```

# Combine ESS data with CHES data----

Data selection and recoding.
```{r add ches}


ess <- recode_missings(ess_raw)
ess <- as.data.frame(ess_raw %>% select("essround",
                      "idno","cntry", 
                      "gndr","agea", "vote",
                      "yrbrn", "domicil", "maritalb", 
                      "rlgdgr", "eisced", "edulvlb", "blgetmg",
                      "imbgeco", "imueclt", "imwbcnt", 
                      "psppipla", "psppsgva",
                      "chldhhe", "lrscale", "stfeco",
                      "ipbhprp","impsafe","ipstrgv","imptrad","ipfrule",
                      "impdiff", "ipadvnt", "ipcrtiv", "impfree","ipudrst",
                      "uemp3m", "uemp12m", "hinctnta", "hincfel",
                      "isco08", "emplrel", "emplno",
                      "isco08p", "emprelp",
                      "dweight", "pweight",
                      starts_with("trst"),
                      starts_with("prtv"),
                      starts_with("rship"),
                      -ends_with("de1")))

# Create one party vote variable
ess <- ess %>%
  unite("party", prtvtcat:prtvtdsk, na.rm=TRUE, remove=FALSE)

# Party IDs to match ESS and CHES data
partyid <- read.xlsx(here("raw-data/ches_party_list.xlsx"))
partyid$party <- as.character(partyid$party)
ess$cntry <- as.character(ess$cntry)
ess <- left_join(ess, partyid, by=c("cntry", "party"))

# CHES data
ches <- read.csv("https://www.chesdata.eu/s/CHES2019V3.csv", 
                          fileEncoding = "UTF-8")

ches$auth <- scales::rescale((ches$galtan + ches$nationalism + ches$civlib_laworder + 
                                ches$multiculturalism + ches$sociallifestyle +
                                ches$immigrate_policy + ches$ethnic_minorities), 
                             to = c(0,100))

ches$pop <- scales::rescale((ches$antielite_salience + ches$corrupt_salience), 
                            to = c(0,100))


ches$lr <- scales::rescale((ches$deregulation + ches$econ_interven +
                              ches$redistribution + ches$spendvtax),
                            to = c(0,100))

ches <- ches %>%
  select(country, party_id, auth, pop, lr)%>%
  rename(country_ches = "country",
         party_id_ches = "party_id")

ches$country <- factor(ches$country_ches,
         levels = c(1,2,3,4,5,6,7,8,10,11,12,13,14,16,
                    20,21,22,23,24,25,26,27,28,29, 31, 34,35,36,37,
                    38,40,45),
         labels = c("BE","DK", "DE", "GR", "ES", "FR", "IE", "IT", "NL", "GB", "PT",
                    "AT", "FI", "SE", "BG", "CZ", "EE", "HU", "LV", "LT", "PL",
                    "RO", "SK", "SI", "HR", "TU", "NO", "CH", "MT", "LU", "CY", "IS"))

ess$country <- ess$cntry
ess <- left_join(ess, ches, by=c("country", "party_id_ches"))

ess <- ess %>%
  select(-starts_with("prtvt"))

# authoritarian populist
ess <- ess %>%
  mutate(authpop = case_when(
    auth > 50 & pop > 50 ~1,
    auth < 50 & pop > 50 ~0,
    auth < 50 & pop < 50 ~0,
    auth > 50 & pop < 50 ~0,
  ))

# libertarian populists
ess <- ess %>%
  mutate(libpop = case_when(
    auth < 50 & pop > 50 ~1,
    auth > 50 & pop > 50 ~0,
    auth < 50 & pop < 50 ~0,
    auth > 50 & pop < 50 ~0,
  ))

ess$popauth <-  sqrt(ess$pop*ess$auth)

```

# Recode variables for subsequent use----

Recode variables
```{r recode}

## Demographics

# Cohorts
ess$yrbrn <- ifelse(ess$yrbrn<1900, NA, ess$yrbrn)
ess$yrbrn <- ifelse(ess$yrbrn>1996, NA, ess$yrbrn)

ess <- ess %>%
  mutate(cohort = case_when(
    yrbrn %in% 1900:1945 ~ "1",
    yrbrn %in% 1946:1964 ~ "2",
    yrbrn %in% 1965:1979 ~ "3",
    yrbrn %in% 1980:1996 ~ "4"
  ))

ess$cohort <- factor(ess$cohort,
                     levels=c(1:4),
                     labels=c("Interwar",
                              "Baby Boomers",
                              "Gen X",
                              "Millennials"))

ess$age <- ifelse(ess$agea==999, NA, ess$agea)

ess$gndr <- ifelse(ess$gndr>2, NA, ess$gndr)
ess <- ess %>%
  mutate(gender = case_when(
    gndr == 1 ~ 1,
    gndr == 2 ~ 0))


ess$uemp3m[ess$uemp3m==2] <- 0
ess$unemp <- if_else(is.na(ess$uemp12m), ess$uemp3m, ess$uemp12m)
ess$unemp <- factor(ess$unemp,
                    levels=c(0,1,2),
                    labels=c("never unemployed",
                             "3 months",
                             "12 months"))

ess <- ess %>%
  mutate(married = ifelse(maritalb<3,1,0),
        separate = case_when(
          maritalb==3 ~1,
          maritalb==4 ~1,
          maritalb==1 | maritalb==2~0,
          maritalb==5 | maritalb==6~0))

#ess$edulvlb <- ifelse(ess$edulvlb>5,NA,ess$edulvlb)
ess$eisced <- ifelse(ess$eisced>7, NA, ess$eisced)

ess$edulvlb[ess$edulvlb>800] <- NA
ess <- ess %>%
  mutate(edu5 = case_when(
    edulvlb < 200 ~1,
    edulvlb >199 & edulvlb <300~2,
    edulvlb >299 & edulvlb <400~3,
    edulvlb >399 & edulvlb <500~4,
    edulvlb >499 & edulvlb <7777~5))

ess <- ess %>%
  mutate(edu3 = case_when(
      edu5==1 | edu5==2~1,
      edu5==3 ~2,
      edu5==4 | edu5==5~3))


ess$urban <- ifelse(ess$domicil>5, NA, ess$domicil)

ess$relig <- ifelse(ess$rlgdgr>10, NA, ess$rlgdgr)

# children
ess <- ess %>%
  mutate(children = case_when(
    rshipa2==2 ~1,
    rshipa3==2 ~1,
    rshipa4==2 ~1,
    rshipa5==2 ~1,
    rshipa6==2 ~1,
    rshipa7==2 ~1,
    rshipa8==2 ~1,
    rshipa9==2 ~1,
    rshipa10==2 ~1,
    rshipa2!=2 ~0,
    rshipa3!=2 ~0,
    rshipa4!=2 ~0,
    rshipa5!=2 ~0,
    rshipa6!=2 ~0,
    rshipa7!=2 ~0,
    rshipa8!=2 ~0,
    rshipa9!=2 ~0,
    rshipa10!=2 ~0))

ess$chldhhe <- if_else(ess$chldhhe==1,1,0)
ess$chldhhe <- as.numeric(ess$chldhhe)
ess$children <- as.numeric(ess$children)
ess$children <- if_else(is.na(ess$children), ess$chldhhe, ess$children)

# Ethnic minority
ess$ethnic <- if_else(ess$blgetmg==1,1,0)

# Dissatisfied with state of the economy
ess$diseco <- 10-ess$stfeco

## Schwartz scale authoritarian values 0 to 100
ess$behave <- as.numeric(ess$ipbhprp)
ess$secure <- as.numeric(ess$impsafe)
ess$safety <- as.numeric(ess$ipstrgv)
ess$tradition <-  as.numeric(ess$imptrad)
ess$rules <- as.numeric(ess$ipfrule)

ess$schwartzauth <- scales::rescale(ess$secure + ess$rules + ess$behave +
                                      ess$safety + ess$tradition, na.rm=T, to=c(100,0))

ess$weight <- ess$dweight * ess$pweight

# Political trust
ess$trstplt <- as.numeric(ess$trstplt)
ess$trstprl <- as.numeric(ess$trstprl)
ess$trstprt <- as.numeric(ess$trstprt)

ess$trstplt <- ifelse(ess$trstplt>10, NA, ess$trstplt)
ess$trstprl <- ifelse(ess$trstprl>10, NA, ess$trstprl)
ess$trstprt <- ifelse(ess$trstprt>10, NA, ess$trstprt)

ess$trust <- scales::rescale(ess$trstplt + ess$trstprl + ess$trstprt, na.rm=T, to=c(0,100))

ess$vote[ess$vote==3] <- NA
ess$vote[ess$vote==2] <- 0

ess$psppipla <- ifelse(ess$psppipla>5, NA, ess$psppipla)
ess$psppsgva <- ifelse(ess$psppsgva>5, NA, ess$psppsgva)
ess$imueclt <- ifelse(ess$imueclt>10, NA, ess$imueclt)
ess$imbgeco <- ifelse(ess$imbgeco>10, NA, ess$imbgeco)
ess$imwbcnt <- ifelse(ess$imwbcnt>10, NA, ess$imwbcnt)

ess$immig <- scales::rescale(ess$imueclt + ess$imbgeco + ess$imwbcnt, na.rm=T, to=c(0,100))
ess$efficacy <- scales::rescale(ess$psppipla + ess$psppsgva, na.rm=T, to=c(0,100))

```


# Add Oesch class scheme----

```{r add class scheme}

## respondent's class
ess$isco_mainjob <- ess$isco08

ess$emplrel_r <- ess$emplrel

ess <- ess %>%
  mutate(emplno_r = case_when(
    emplno==0 ~0,
    is.na(emplno) ~ 0,
    emplno>0 & emplno<10~1,
    emplno>10 & emplno>10001~2))

ess$selfem_mainjob <- NA
ess <- ess %>%
  mutate(selfem_mainjob = case_when(
    emplrel_r==1~ 1,
    is.na(emplrel_r)~ 1,
    emplrel_r==3~ 2,
    emplrel_r==2 & emplno_r==0~ 2,
    emplrel_r==2 & emplno_r==1~ 3,
    emplrel_r==2 & emplno_r==2~ 4))

## 16 category class scheme
ess$class16_r <- NA
ess$class16_r[ess$selfem_mainjob==4] <- 1
ess$class16_r[(ess$selfem_mainjob==2 | ess$selfem_mainjob==3) & 
                (ess$isco_mainjob >= 2000 & ess$isco_mainjob <= 2162)] <- 2
ess$class16_r[(ess$selfem_mainjob==2 | ess$selfem_mainjob==3) & 
                (ess$isco_mainjob >= 2164 & ess$isco_mainjob <= 2165)]  <- 2
ess$class16_r[(ess$selfem_mainjob==2 | ess$selfem_mainjob==3) & 
                (ess$isco_mainjob >= 2200 & ess$isco_mainjob <= 2212)]  <- 2
ess$class16_r[(ess$selfem_mainjob==2 | ess$selfem_mainjob==3) & 
                (ess$isco_mainjob == 2250)]  <- 2
ess$class16_r[(ess$selfem_mainjob==2 | ess$selfem_mainjob==3) & 
                (ess$isco_mainjob >= 2261 & ess$isco_mainjob <= 2262)]  <- 2
ess$class16_r[(ess$selfem_mainjob==2 | ess$selfem_mainjob==3) &
                (ess$isco_mainjob >= 2300 & ess$isco_mainjob <= 2330)]  <- 2
ess$class16_r[(ess$selfem_mainjob==2 | ess$selfem_mainjob==3) & 
                (ess$isco_mainjob >= 2350 & ess$isco_mainjob <= 2352)]  <- 2
ess$class16_r[(ess$selfem_mainjob==2 | ess$selfem_mainjob==3) & 
                (ess$isco_mainjob >= 2359 & ess$isco_mainjob <= 2432)]  <- 2
ess$class16_r[(ess$selfem_mainjob==2 | ess$selfem_mainjob==3) & 
                (ess$isco_mainjob >= 2500 & ess$isco_mainjob <= 2619)]  <- 2
ess$class16_r[(ess$selfem_mainjob==2 | ess$selfem_mainjob==3) & 
                (ess$isco_mainjob == 2621)]  <- 2
ess$class16_r[(ess$selfem_mainjob==2 | ess$selfem_mainjob==3) & 
                (ess$isco_mainjob >= 2630 & ess$isco_mainjob <= 2634)]  <- 2
ess$class16_r[(ess$selfem_mainjob==2 | ess$selfem_mainjob==3) & 
                (ess$isco_mainjob >= 2636 & ess$isco_mainjob <= 2640)]  <- 2
ess$class16_r[(ess$selfem_mainjob==2 | ess$selfem_mainjob==3) & 
                (ess$isco_mainjob >= 2642 & ess$isco_mainjob <= 2643)]  <- 2

# Small business owners with employees (3)
ess$class16_r[(ess$selfem_mainjob==3) & (ess$isco_mainjob >= 1000 & ess$isco_mainjob <= 1439)] <- 3
ess$class16_r[(ess$selfem_mainjob==3) & (ess$isco_mainjob == 2163)] <- 3
ess$class16_r[(ess$selfem_mainjob==3) & (ess$isco_mainjob == 2166)] <- 3
ess$class16_r[(ess$selfem_mainjob==3) & (ess$isco_mainjob >= 2220 & ess$isco_mainjob <= 2240)] <- 3
ess$class16_r[(ess$selfem_mainjob==3) & (ess$isco_mainjob == 2260)] <- 3
ess$class16_r[(ess$selfem_mainjob==3) & (ess$isco_mainjob >= 2263 & ess$isco_mainjob <= 2269)] <- 3
ess$class16_r[(ess$selfem_mainjob==3) & (ess$isco_mainjob >= 2340 & ess$isco_mainjob <= 2342)] <- 3
ess$class16_r[(ess$selfem_mainjob==3) & (ess$isco_mainjob >= 2353 & ess$isco_mainjob <= 2356)] <- 3
ess$class16_r[(ess$selfem_mainjob==3) & (ess$isco_mainjob >= 2433 & ess$isco_mainjob <= 2434)] <- 3
ess$class16_r[(ess$selfem_mainjob==3) & (ess$isco_mainjob == 2620)] <- 3
ess$class16_r[(ess$selfem_mainjob==3) & (ess$isco_mainjob == 2622)] <- 3
ess$class16_r[(ess$selfem_mainjob==3) & (ess$isco_mainjob == 2635)] <- 3
ess$class16_r[(ess$selfem_mainjob==3) & (ess$isco_mainjob == 2641)] <- 3
ess$class16_r[(ess$selfem_mainjob==3) & (ess$isco_mainjob >= 2650 & ess$isco_mainjob <= 2659)] <- 3
ess$class16_r[(ess$selfem_mainjob==3) & (ess$isco_mainjob >= 3000 & ess$isco_mainjob <= 9629)] <- 3

# Small business owners without employees (4)

ess$class16_r[(ess$selfem_mainjob==2) & (ess$isco_mainjob >= 1000 & ess$isco_mainjob <= 1439)]  <- 4
ess$class16_r[(ess$selfem_mainjob==2) & (ess$isco_mainjob == 2163)] <- 4
ess$class16_r[(ess$selfem_mainjob==2) & (ess$isco_mainjob == 2166)] <- 4
ess$class16_r[(ess$selfem_mainjob==2) & (ess$isco_mainjob >= 2220 & ess$isco_mainjob <= 2240)] <- 4
ess$class16_r[(ess$selfem_mainjob==2) & (ess$isco_mainjob == 2260)] <- 4
ess$class16_r[(ess$selfem_mainjob==2) & (ess$isco_mainjob >= 2263 & ess$isco_mainjob <= 2269)] <- 4
ess$class16_r[(ess$selfem_mainjob==2) & (ess$isco_mainjob >= 2340 & ess$isco_mainjob <= 2342)] <- 4
ess$class16_r[(ess$selfem_mainjob==2) & (ess$isco_mainjob >= 2353 & ess$isco_mainjob <= 2356)] <- 4
ess$class16_r[(ess$selfem_mainjob==2) & (ess$isco_mainjob >= 2433 & ess$isco_mainjob <= 2434)] <- 4
ess$class16_r[(ess$selfem_mainjob==2) & (ess$isco_mainjob == 2620)] <- 4
ess$class16_r[(ess$selfem_mainjob==2) & (ess$isco_mainjob == 2622)] <- 4
ess$class16_r[(ess$selfem_mainjob==2) & (ess$isco_mainjob == 2635)] <- 4
ess$class16_r[(ess$selfem_mainjob==2) & (ess$isco_mainjob == 2641)] <- 4
ess$class16_r[(ess$selfem_mainjob==2) & (ess$isco_mainjob >= 2650 & ess$isco_mainjob <= 2659)] <- 4
ess$class16_r[(ess$selfem_mainjob==2) & (ess$isco_mainjob >= 3000 & ess$isco_mainjob <= 9629)] <- 4

# Technical experts (5)
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob >= 2100 & ess$isco_mainjob <= 2162)] <- 5
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob >= 2164 & ess$isco_mainjob <= 2165)] <- 5
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob >= 2500 & ess$isco_mainjob <= 2529)] <- 5

# Technicians (6)
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob >= 3100 & ess$isco_mainjob <= 3155)] <- 6
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob >= 3210 & ess$isco_mainjob <= 3214)] <- 6
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob == 3252)] <- 6
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob >= 3500 & ess$isco_mainjob <= 3522)] <- 6

# Skilled manual (7)
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob >= 6000 & ess$isco_mainjob <= 7549)] <- 7
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob >= 8310 & ess$isco_mainjob <= 8312)] <- 7
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob == 8330)] <- 7
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob >= 8332 & ess$isco_mainjob <= 8340)] <- 7
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob >= 8342 & ess$isco_mainjob <= 8344)] <- 7

# Low-skilled manual (8)
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob >= 8000 & ess$isco_mainjob <= 8300)] <- 8
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob >= 8320 & ess$isco_mainjob <= 8321)] <- 8
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob == 8341)] <- 8
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob == 8350)] <- 8
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob >= 9200 & ess$isco_mainjob <= 9334)] <- 8
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob >= 9600 & ess$isco_mainjob <= 9620)] <- 8
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob >= 9622 & ess$isco_mainjob <= 9629)] <- 8

# Higher-grade managers and administrators (9)
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob >= 1000 & ess$isco_mainjob <= 1300)] <- 9
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob >= 1320 & ess$isco_mainjob <= 1349)] <- 9
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob >= 2400 & ess$isco_mainjob <= 2432)] <- 9
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob >= 2610 & ess$isco_mainjob <= 2619)] <- 9
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob == 2631)] <- 9
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob >= 100 & ess$isco_mainjob <= 110)] <- 9

# Lower-grade managers and administrators (10)
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob >= 1310 & ess$isco_mainjob <= 1312)] <- 10
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob >= 1400 & ess$isco_mainjob <= 1439)] <- 10
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob >= 2433 & ess$isco_mainjob <= 2434)] <- 10
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob >= 3300 & ess$isco_mainjob <= 3339)] <- 10
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob == 3343)] <- 10
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob >= 3350 & ess$isco_mainjob <= 3359)] <- 10
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob == 3411)] <- 10
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob == 5221)] <- 10
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob >= 200 & ess$isco_mainjob <= 210)] <- 10

# Skilled clerks (11)
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob >= 3340 & ess$isco_mainjob <= 3342)] <- 11
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob == 3344)] <- 11
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob >= 4000 & ess$isco_mainjob <= 4131)] <- 11
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob >= 4200 & ess$isco_mainjob <= 4221)] <- 11
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob >= 4224 & ess$isco_mainjob <= 4413)] <- 11
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob >= 4415 & ess$isco_mainjob <= 4419)] <- 11

# Unskilled clerks (12)
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob == 4132)] <- 12
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob == 4222)] <- 12
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob == 4223)] <- 12
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob == 5230)] <- 12
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob == 9621)] <- 12

# Socio-cultural professionals (13)
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob >= 2200 &  ess$isco_mainjob <= 2212)] <- 13
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob == 2250)] <- 13
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob >= 2261 &  ess$isco_mainjob <= 2262)] <- 13
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob >= 2300 &  ess$isco_mainjob <= 2330)] <- 13
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob >= 2350 &  ess$isco_mainjob <= 2352)] <- 13
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob == 2359)] <- 13
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob == 2600)] <- 13
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob == 2621)] <- 13
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob == 2630)] <- 13
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob >= 2632 &  ess$isco_mainjob <= 2634)] <- 13
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob >= 2636 &  ess$isco_mainjob <= 2640)] <- 13
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob >= 2642 &  ess$isco_mainjob <= 2643)] <- 13


# Socio-cultural semi-professionals (14)
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob == 2163)] <- 14
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob == 2166)] <- 14
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob >= 2220 & ess$isco_mainjob <= 2240)] <- 14
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob == 2260)] <- 14
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob >= 2263 & ess$isco_mainjob <= 2269)] <- 14
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob >= 2340 & ess$isco_mainjob <= 2342)] <- 14
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob >= 2353 & ess$isco_mainjob <= 2356)] <- 14
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob == 2620)] <- 14
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob == 2622)] <- 14
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob == 2635)] <- 14
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob == 2641)] <- 14
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob >= 2650 & ess$isco_mainjob <= 2659)] <- 14
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob == 3200)] <- 14
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob >= 3220 & ess$isco_mainjob <= 3230)] <- 14
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob == 3250)] <- 14
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob >= 3253 & ess$isco_mainjob <= 3257)] <- 14
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob == 3259)] <- 14
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob >= 3400 & ess$isco_mainjob <= 3410)] <- 14
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob >= 3412 & ess$isco_mainjob <= 3413)] <- 14
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob >= 3430 & ess$isco_mainjob <= 3433)] <- 14
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob == 3435)] <- 14
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob == 4414)] <- 14

# Skilled service (15)
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob == 3240)] <- 15
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob == 3251)] <- 15
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob == 3258)] <- 15
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob >= 3420 & ess$isco_mainjob <= 3423)] <- 15
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob == 3434)] <- 15
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob >= 5000 & ess$isco_mainjob <= 5120)] <- 15
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob >= 5140 & ess$isco_mainjob <= 5142)] <- 15
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob == 5163)] <- 15
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob == 5165)] <- 15
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob == 5200)] <- 15
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob == 5220)] <- 15
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob >= 5222 & ess$isco_mainjob <= 5223)] <- 15
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob >= 5241 & ess$isco_mainjob <= 5242)] <- 15
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob >= 5300 & ess$isco_mainjob <= 5321)] <- 15
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob >= 5400 & ess$isco_mainjob <= 5413)] <- 15
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob == 5419)] <- 15
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob == 8331)] <- 15

# Low-skilled service (16)
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob >= 5130 & ess$isco_mainjob <= 5132)] <- 16
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob >= 5150 & ess$isco_mainjob <= 5162)] <- 16
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob == 5164)] <- 16
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob == 5169)] <- 16
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob >= 5210 & ess$isco_mainjob <= 5212)] <- 16
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob == 5240)] <- 16
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob >= 5243 & ess$isco_mainjob <= 5249)] <- 16
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob >= 5322 & ess$isco_mainjob <= 5329)] <- 16
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob == 5414)] <- 16
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob == 8322)] <- 16
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob >= 9100 & ess$isco_mainjob <= 9129)] <- 16
ess$class16_r[(ess$selfem_mainjob==1) & (ess$isco_mainjob >= 9400 & ess$isco_mainjob <= 9520)] <- 16

# Class 8 & Class 5
ess <- ess %>%
  mutate(class8_r = case_when(
    class16_r==1 | class16_r==2~ 1,
    class16_r==3 | class16_r==4~ 2,
    class16_r==5 | class16_r==6~ 3,
    class16_r==7 | class16_r==8~ 4,
    class16_r==9 | class16_r==10~ 5,
    class16_r==11 | class16_r==12 ~6,
    class16_r==13 | class16_r==14 ~7,
    class16_r==15 | class16_r==16~ 8),
    class5_r = case_when(
    class16_r==1 | class16_r==2 | class16_r==5 | class16_r==9 | class16_r==13 ~ 1,
    class16_r==6 | class16_r==10 | class16_r==14 ~ 2,
    class16_r==3 | class16_r==4 ~ 3,
    class16_r==7 | class16_r==11 | class16_r==15 ~ 4,
    class16_r==8 | class16_r==12 | class16_r==16 ~ 5))


## partner's class
ess$isco_partner <- ess$isco08p

ess <- ess %>%
  mutate(selfem_partner = case_when(
    emprelp ==1~ 1,
    emprelp ==2 | emprelp==3~ 2,
    is.na(emprelp)~ 1))


ess$class16_p  <- NA

# Self-employed professionals (2)
ess$class16_p[(ess$selfem_partner==2) & (ess$isco_partner >= 2000 & ess$isco_partner <= 2162)] <- 2
ess$class16_p[(ess$selfem_partner==2) & (ess$isco_partner >= 2164 & ess$isco_partner <= 2165)] <- 2
ess$class16_p[(ess$selfem_partner==2) & (ess$isco_partner >= 2200 & ess$isco_partner <= 2212)] <- 2
ess$class16_p[(ess$selfem_partner==2) & (ess$isco_partner == 2250)] <- 2
ess$class16_p[(ess$selfem_partner==2) & (ess$isco_partner >= 2261 & ess$isco_partner <= 2262)] <- 2
ess$class16_p[(ess$selfem_partner==2) & (ess$isco_partner >= 2300 & ess$isco_partner <= 2330)] <- 2
ess$class16_p[(ess$selfem_partner==2) & (ess$isco_partner >= 2350 & ess$isco_partner <= 2352)] <- 2
ess$class16_p[(ess$selfem_partner==2) & (ess$isco_partner >= 2359 & ess$isco_partner <= 2432)] <- 2
ess$class16_p[(ess$selfem_partner==2) & (ess$isco_partner >= 2500 & ess$isco_partner <= 2619)] <- 2
ess$class16_p[(ess$selfem_partner==2) & (ess$isco_partner == 2621)] <- 2
ess$class16_p[(ess$selfem_partner==2) & (ess$isco_partner >= 2630 & ess$isco_partner <= 2634)] <- 2
ess$class16_p[(ess$selfem_partner==2) & (ess$isco_partner >= 2636 & ess$isco_partner <= 2640)] <- 2
ess$class16_p[(ess$selfem_partner==2) & (ess$isco_partner >= 2642 & ess$isco_partner <= 2643)] <- 2

# Small business owners without employees (4)
ess$class16_p[(ess$selfem_partner==2) & (ess$isco_partner >= 1000 & ess$isco_partner <= 1439)] <- 4
ess$class16_p[(ess$selfem_partner==2) & (ess$isco_partner == 2163)] <- 4
ess$class16_p[(ess$selfem_partner==2) & (ess$isco_partner == 2166)] <- 4
ess$class16_p[(ess$selfem_partner==2) & (ess$isco_partner >= 2220 & ess$isco_partner <= 2240)] <- 4
ess$class16_p[(ess$selfem_partner==2) & (ess$isco_partner == 2260)] <- 4
ess$class16_p[(ess$selfem_partner==2) & (ess$isco_partner >= 2263 & ess$isco_partner <= 2269)] <- 4
ess$class16_p[(ess$selfem_partner==2) & (ess$isco_partner >= 2340 & ess$isco_partner <= 2342)] <- 4
ess$class16_p[(ess$selfem_partner==2) & (ess$isco_partner >= 2353 & ess$isco_partner <= 2356)] <- 4
ess$class16_p[(ess$selfem_partner==2) & (ess$isco_partner >= 2433 & ess$isco_partner <= 2434)] <- 4
ess$class16_p[(ess$selfem_partner==2) & (ess$isco_partner == 2620)] <- 4
ess$class16_p[(ess$selfem_partner==2) & (ess$isco_partner == 2622)] <- 4
ess$class16_p[(ess$selfem_partner==2) & (ess$isco_partner == 2635)] <- 4
ess$class16_p[(ess$selfem_partner==2) & (ess$isco_partner == 2641)] <- 4
ess$class16_p[(ess$selfem_partner==2) & (ess$isco_partner >= 2650 & ess$isco_partner <= 2659)] <- 4
ess$class16_p[(ess$selfem_partner==2) & (ess$isco_partner >= 3000 & ess$isco_partner <= 9629)] <- 4

# Technical experts (5)
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner >= 2100 & ess$isco_partner <= 2162)] <- 5
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner >= 2164 & ess$isco_partner <= 2165)] <- 5
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner >= 2500 & ess$isco_partner <= 2529)] <- 5

# Technicians (6)
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner >= 3100 & ess$isco_partner <= 3155)] <- 6
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner >= 3210 & ess$isco_partner <= 3214)] <- 6
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner == 3252)] <- 6
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner >= 3500 & ess$isco_partner <= 3522)] <- 6

# Skilled manual (7)
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner >= 6000 & ess$isco_partner <= 7549)] <- 7
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner >= 8310 & ess$isco_partner <= 8312)] <- 7
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner == 8330)] <- 7
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner >= 8332 & ess$isco_partner <= 8340)] <- 7
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner >= 8342 & ess$isco_partner <= 8344)] <- 7

# Low-skilled manual (8)
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner >= 8000 & ess$isco_partner <= 8300)] <- 8
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner >= 8320 & ess$isco_partner <= 8321)] <- 8
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner == 8341)] <- 8
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner == 8350)] <- 8
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner >= 9200 & ess$isco_partner <= 9334)] <- 8
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner >= 9600 & ess$isco_partner <= 9620)] <- 8
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner >= 9622 & ess$isco_partner <= 9629)] <- 8

# Higher-grade managers and administrators (9)
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner >= 1000 & ess$isco_partner <= 1300)] <- 9
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner >= 1320 & ess$isco_partner <= 1349)] <- 9
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner >= 2400 & ess$isco_partner <= 2432)] <- 9
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner >= 2610 & ess$isco_partner <= 2619)] <- 9
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner == 2631)] <- 9
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner >= 100 & ess$isco_partner <= 110)] <- 9

# Lower-grade managers and administrators (10)
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner >= 1310 & ess$isco_partner <= 1312)] <- 10
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner >= 1400 & ess$isco_partner <= 1439)] <- 10
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner >= 2433 & ess$isco_partner <= 2434)] <- 10
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner >= 3300 & ess$isco_partner <= 3339)] <- 10
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner == 3343)] <- 10
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner >= 3350 & ess$isco_partner <= 3359)] <- 10
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner == 3411)] <- 10
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner == 5221)] <- 10
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner >= 200 & ess$isco_partner <= 210)] <- 10

# Skilled clerks (11)
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner >= 3340 & ess$isco_partner <= 3342)] <- 11
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner == 3344)] <- 11
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner >= 4000 & ess$isco_partner <= 4131)] <- 11
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner >= 4200 & ess$isco_partner <= 4221)] <- 11
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner >= 4224 & ess$isco_partner <= 4413)] <- 11
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner >= 4415 & ess$isco_partner <= 4419)] <- 11

# Unskilled clerks (12)
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner == 4132)] <- 12
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner == 4222)] <- 12
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner == 4223)] <- 12
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner == 5230)] <- 12
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner == 9621)] <- 12

# Socio-cultural professionals (13)
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner >= 2200 & ess$isco_partner <= 2212)] <- 13
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner == 2250)] <- 13
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner >= 2261 & ess$isco_partner <= 2262)] <- 13
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner >= 2300 & ess$isco_partner <= 2330)] <- 13
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner >= 2350 & ess$isco_partner <= 2352)] <- 13
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner == 2359)] <- 13
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner == 2600)] <- 13
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner == 2621)] <- 13
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner == 2630)] <- 13
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner >= 2632 & ess$isco_partner <= 2634)] <- 13
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner >= 2636 & ess$isco_partner <= 2640)] <- 13
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner >= 2642 & ess$isco_partner <= 2643)] <- 13

# Socio-cultural semi-professionals (14)
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner == 2163)] <- 14
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner == 2166)] <- 14
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner >= 2220 & ess$isco_partner <= 2240)] <- 14
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner == 2260)] <- 14
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner >= 2263 & ess$isco_partner <= 2269)] <- 14
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner >= 2340 & ess$isco_partner <= 2342)] <- 14
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner >= 2353 & ess$isco_partner <= 2356)] <- 14
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner == 2620)] <- 14
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner == 2622)] <- 14
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner == 2635)] <- 14
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner == 2641)] <- 14
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner >= 2650 & ess$isco_partner <= 2659)] <- 14
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner == 3200)] <- 14
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner >= 3220 & ess$isco_partner <= 3230)] <- 14
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner == 3250)] <- 14
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner >= 3253 & ess$isco_partner <= 3257)] <- 14
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner == 3259)] <- 14
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner >= 3400 & ess$isco_partner <= 3410)] <- 14
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner >= 3412 & ess$isco_partner <= 3413)] <- 14
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner >= 3430 & ess$isco_partner <= 3433)] <- 14
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner == 3435)] <- 14
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner == 4414)] <- 14

# Skilled service (15)
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner == 3240)] <- 15
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner == 3251)] <- 15
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner == 3258)] <- 15
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner >= 3420 & ess$isco_partner <= 3423)] <- 15
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner == 3434)] <- 15
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner >= 5000 & ess$isco_partner <= 5120)] <- 15
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner >= 5140 & ess$isco_partner <= 5142)] <- 15
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner == 5163)] <- 15
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner == 5165)] <- 15
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner == 5200)] <- 15
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner == 5220)] <- 15
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner >= 5222 & ess$isco_partner <= 5223)] <- 15
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner >= 5241 & ess$isco_partner <= 5242)] <- 15
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner >= 5300 & ess$isco_partner <= 5321)] <- 15
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner >= 5400 & ess$isco_partner <= 5413)] <- 15
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner == 5419)] <- 15
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner == 8331)] <- 15

# Low-skilled service (16)
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner >= 5130 & ess$isco_partner <= 5132)] <- 16
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner >= 5150 & ess$isco_partner <= 5162)] <- 16
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner == 5164)] <- 16
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner == 5169)] <- 16
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner >= 5210 & ess$isco_partner <= 5212)] <- 16
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner == 5240)] <- 16
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner >= 5243 & ess$isco_partner <= 5249)] <- 16
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner >= 5322 & ess$isco_partner <= 5329)] <- 16
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner == 5414)] <- 16
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner == 8322)] <- 16
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner >= 9100 & ess$isco_partner <= 9129)] <- 16
ess$class16_p[(ess$selfem_partner==1) & (ess$isco_partner >= 9400 & ess$isco_partner <= 9520)] <- 16

# Class 8 & Class 5
ess <- ess %>%
  mutate(class8_p = case_when(
    class16_p==1 | class16_p==2~ 1,
    class16_p==3 | class16_p==4~ 2,
    class16_p==5 | class16_p==6~ 3,
    class16_p==7 | class16_p==8~ 4,
    class16_p==9 | class16_p==10~ 5,
    class16_p==11 | class16_p==12 ~6,
    class16_p==13 | class16_p==14 ~7,
    class16_p==15 | class16_p==16~ 8),
    class5_p = case_when(
    class16_p==1 | class16_p==2 | class16_p==5 | class16_p==9 | class16_p==13 ~ 1,
    class16_p==6 | class16_p==10 | class16_p==14 ~ 2,
    class16_p==3 | class16_p==4 ~ 3,
    class16_p==7 | class16_p==11 | class16_p==15 ~ 4,
    class16_p==8 | class16_p==12 | class16_p==16 ~ 5))


ess$class16 <- if_else(is.na(ess$class16_r),ess$class16_p, ess$class16_r)
ess$class8 <- if_else(is.na(ess$class8_r),ess$class8_p, ess$class8_r)
ess$class5 <- if_else(is.na(ess$class5_r),ess$class5_p, ess$class5_r)

ess$class8_fac <- factor(ess$class8,
                     levels=c(1:8),
                     labels=c("Self-employed professionals and large employers",
                              "Small business owners",
                              "Technical (semi-)professionals",
                              "Production workers",
                              "(Associate) managers",
                              "Clerks",
                              "Socio-cultural (semi-)professionals",
                              "Service workers"))

ess$class5 <- factor(ess$class5,
                     levels=c(1:5),
                     labels=c("Higher-grade service class",
                              "Lower-grade service class",
                              "Small business owners",
                              "Skilled workers",
                              "Unskilled workers"))

ess$class16_fac <- factor(ess$class16,
                          levels=c(1:16),
                          labels=c("Large employers", 
                                   "Self-employed professionals",
                                   "Small business owners with employees",
                                   "Small business owners without employees",
                                   "Technical experts",
                                   "Technicians",
                                   "Skilled manual",
                                   "Low-skilled manual",
                                   "Higher-grade managers",
                                   "Lower-grade managers",
                                   "Skilled clerks",
                                   "Unskilled clerks",
                                   "Socio-cultural professionals",
                                   "Socio-cultural semi-professionals",
                                   "Skilled service",
                                   "Low-skilled service"))

ess$schwartz01 <- scales::rescale(ess$schwartzauth, na.rm=T, to=c(0,1))

ess$efficacy01 <- scales::rescale(ess$efficacy, na.rm=T, to=c(0,1))

ess$immig01 <- scales::rescale(ess$immig, na.rm=T, to=c(0,1))

ess$trust01  <- scales::rescale(ess$trust, na.rm=T, to=c(0,1))

saveRDS(ess, file = (here("processed-data", "ess9.rds")))

```